﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_detail : System.Web.UI.Page
{
    protected Topic parent;
    protected string path = string.Empty;
    protected Article art;
    protected void Page_Load(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();
        art = new Article();
        ///
        // 初始化
        ///
        var train = (from aT in db.Train orderby aT.Registed descending select aT).Take(6);
        Repeater1.DataSource = train;
        Repeater1.DataBind();
        var px2 = (from aA in db.Article where aA.Verified != null orderby aA.Verified descending select aA).Take(6);
        Repeater2.DataSource = px2;
        Repeater2.DataBind();
        //
        //获取栏目id
        //
        string article_id = Request.QueryString["article_id"];
        //
        //获取栏目id
        //
        string topic_id = (from aA in db.Article where aA.ID.Equals(article_id) select aA.TopicID).First();
        parent = (from aT in db.Topic where aT.ID.Equals(topic_id) select aT).First();
        path = parent.Name;
        while (GetParent(parent.ID) != null)
        {
            parent = GetParent(parent.ID);
            path = parent.Name + "-->" + path;
        }
        path = "首页-->" + path;
        //
        //获取文章列表
        //
        var a = (from aA in db.Article where aA.ID.Equals(article_id) orderby aA.Published descending, aA.Subtitle select aA).Take(16);
        if (a.Count() != 0)
        {
            art = a.First();
        }

    }
    protected List<Topic> GetChild(string topic_id)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.ParentID.Equals(topic_id) select aT;
        if (t.Count() == 0)
        {
            return null;
        }
        else
        {
            return t.ToList<Topic>();
        }
    }
    protected string GetTopicID(string tName)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.Name.Equals(tName) select aT;
        if (t.Count() != 0)
        {
            return t.First().ID;
        }
        else
        {
            return "";
        }
    }
    private Topic GetParent(string topic_id)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.ID.Equals(topic_id) select aT;
        //var pt=from aT in db.Topic where aT.ParentID.Equals()
        if (t.First().ParentID != null)
        {
            return t.First().Parent;
        }
        else
        {
            return null;
        }
    }
}